﻿using System;
using System.Collections.Generic;
using System.Linq;

using Finance.Entites;
using Finance.Service.Interfaces;

namespace Finance.UI.ViewModels
{
    public class DataContextBuilder
    {
        public static IList<ClassificationDetailGroupViewModel> InitialClassifications<TClassification>(IDataStore<TClassification> source)
            where TClassification : Classification
        {
            IList<ClassificationDetailGroupViewModel> classifications = new List<ClassificationDetailGroupViewModel>();
            IEnumerable<string> groups = source.Items.Select(c => c.Group).Distinct();
            foreach (string group in groups)
            {
                classifications.Add(new ClassificationDetailGroupViewModel()
                {
                    Header = group,
                    Items = new List<DetailItemViewModel>()
                });
            }
            foreach (var item in source.Items)
            {
                var group = classifications.SingleOrDefault(c => c.Header.Equals(item.Group));
                group.Items.Add(new ClassificationDetailItemViewModel()
                {
                    Title = group.Header,
                    Name = item.Name
                });
            }
            return classifications;
        }

        public static IList<AccountDetailGroupViewModel> InitialAccounts()
        {
            IList<AccountDetailGroupViewModel> Accounts = new List<AccountDetailGroupViewModel>();
            IEnumerable<string> groups = App.AccountDataStore.Items.Select(c => c.Group).Distinct();
            foreach (string group in groups)
            {
                Accounts.Add(new AccountDetailGroupViewModel()
                {
                    Header = group,
                    Items = new List<DetailItemViewModel>()
                });
            }
            foreach (var item in App.AccountDataStore.Items)
            {
                var group = Accounts.SingleOrDefault(c => c.Header.Equals(item.Group));
                group.Items.Add(new AccountDetailItemViewModel()
                {
                    Title = group.Header,
                    Name = item.Name
                });
            }

            return Accounts;
        }

        public static IList<TraderDetailGroupViewModel> InitialTraders()
        {
            IList<TraderDetailGroupViewModel> group = new List<TraderDetailGroupViewModel>();

            group.Add(new TraderDetailGroupViewModel()
            {
                Header = "Select Trader",
                Items = new List<DetailItemViewModel>(
                    App.TraderDataStore.Items.Select(
                        item => new TraderDetailItemViewModel()
                        {
                            Title = "Select Trader",
                            Name = item.Name
                        }).Cast<DetailItemViewModel>()),
            });
            return group;
        }

        public static IList<ProjectDetailGroupViewModel> InitialProjects()
        {
            IList<ProjectDetailGroupViewModel> group = new List<ProjectDetailGroupViewModel>();

            group.Add(new ProjectDetailGroupViewModel()
            {
                Header = "Select Project",
                Items = new List<DetailItemViewModel>()
            });
            foreach (var item in App.ProjectDataStore.Items)
            {
                group.First().Items.Add(new ProjectDetailItemViewModel()
                {
                    Title = "Select Project",
                    Name = item.Name
                });
            }
            return group;
        }
    }
}
